<?php
/**
 * Created by PhpStorm.
 * Loan: ASUS
 * Date: 2020/8/24
 * Time: 10:03
 */

namespace app\pcapi\controller;

//登陆的业务逻辑

class Login extends Base
{
    /**
     * @author ASUS
     * @date 2020-08-25 10:27
     * 根据账号和密码登陆
     */
    public function login()
    {
        $admin = input('admin');
        $password = input('password');
        if($admin==''){
            api_return(203,'请输入用户名',[]);
        }
        if($password==''){
            api_return(203,'请输入密码',[]);
        }
        $user = db('admin')->where(['admin'=>$admin])->find();
        if(empty($user)||($user['password']!=md5($password))||($admin!==$user["admin"])){
            api_return(203,'账号或密码错误',[]);
        }
        if($user['status']==0){
            api_return(203,'账号异常',[]);
        }
        if($user["id"]==1){
            $updateInfo = [
                'login_ip'=>get_client_ip(),
                'login_time'=>time(),
            ];
        }else{
            $updateInfo = [
                'login_ip'=>get_client_ip(),
                'login_time'=>time(),
                'token'=>token_create(),
            ];
        }
        db('admin')->where(['admin'=>$admin])->update($updateInfo);
        $field = "*,FROM_UNIXTIME(login_time,'%Y-%m-%d %H:%i:%s') as login_time";
        $info = db('admin')->where(['admin'=>$admin])->field($field)->find();
        cache($admin,$info,7200);
        api_return(200,'成功',$info);
    }
    /**
     * @author ASUS
     * @date 2020-08-25 10:28
     * 退出登陆
     * 清空缓存登陆信息
     */
    public function loginOut()
    {
        $admin = input('admin');
        if($admin==''){
            api_return(203,'参数缺失',[]);
        }
        cache($admin,null);
        api_return('200','成功',[]);
    }
}